import json
import matplotlib.pyplot as plt
import matplotlib
from matplotlib.patches import Ellipse
import numpy as np
from 翻译包 import *
from 文件工具箱 import *
from 句段处理 import *

数据文件 = "./分析结果.json"

def 画散点图(测量指标, 纵坐标范围, 纵坐标标签):
	global 数据文件
	测量结果 = 恢复数据结构(数据文件) 
	数据 = 字典()
	for 算法 in ["sf", "fr", "hf", "hp", "pr", "el"]:
		数据[算法] = []
		for 数据集 in ["caida", "tsinghua", "hgc", "telecom"]:
			for 协议 in ["ipv4", "ipv6"]:
				数据[算法].append(测量结果[".".join([数据集, 协议])][算法][测量指标])
	for 数据集 in ["caida", "tsinghua", "hgc", "telecom"]:
		for 协议 in ["ipv4", "ipv6"]:
			临时变量 = ".".join([数据集, 协议])
			输出("%13s" % 临时变量, end=", ")
			for 算法 in ["sf", "fr", "hf", "hp", "pr", "el"]:
				输出("%s:%.4f" % (算法, 测量结果[临时变量][算法][测量指标]), end = ", ")
			比值1 = (测量结果[临时变量]["sf"][测量指标] - 测量结果[临时变量]["hf"][测量指标])/测量结果[临时变量]["hf"][测量指标]
			比值2 = (测量结果[临时变量]["sf"][测量指标] - 测量结果[临时变量]["hp"][测量指标])/测量结果[临时变量]["hp"][测量指标]
			输出("\nsf/hf:%.4f, sf/hp:%.4f" % (比值1, 比值2))
				

	fig = plt.figure(1)
	ax = fig.add_axes([0,0,1,1])
	ax.set_xlim([0, 9])
	if [] != 纵坐标范围:
		ax.set_ylim(纵坐标范围)
	plt.ylabel(纵坐标标签)
	横坐标 = 范围(1, 9)
	标签 = [r"CAIDA$_{v4}$", r"CAIDA$_{v6}$", r"Cmps$_{v4}$", r"Cmps$_{v6}$", \
		   r"ISP1$_{v4}$", r"ISP1$_{v6}$", r"ISP2$_{v4}$", r"ISP2$_{v6}$"]
	plt.xticks(横坐标, 标签, rotation = 35)
	rects = ax.patches
	图标大小 = 400
	线条宽度 = 2.0
	plt.grid(linestyle = ":")
	plt.scatter(横坐标, 数据["sf"], label = "SF", color = 'red', \
		linewidths = 线条宽度, marker="^", s = 图标大小, facecolors="none")
	plt.scatter(横坐标, 数据["fr"], label = "FR", color = 'orange', \
		linewidths = 线条宽度, marker="s", s = 图标大小, facecolors="none")
	plt.scatter(横坐标, 数据["hf"], label = "HF", color = 'green', \
		linewidths = 线条宽度, marker="o", s = 图标大小, facecolors="none")
	plt.scatter(横坐标, 数据["hp"], label = "HP", color = 'black', \
		linewidths = 线条宽度, marker="P", s = 图标大小, facecolors="none")
	plt.scatter(横坐标, 数据["pr"], label = "PR", color = 'purple', \
		linewidths = 线条宽度, marker="d", s = 图标大小, facecolors="none")
	plt.scatter(横坐标, 数据["el"], label = "EL", color = 'brown', \
		linewidths = 线条宽度, marker="p", s = 图标大小, facecolors="none")
	plt.legend(bbox_to_anchor=(0.0, 1.0, 1.0, 0.1), loc = 3, ncol = 3, mode = "expand", borderaxespad = 0.0)
#	plt.legend(ncol = 2)
	plt.xlabel("Traces")
	plt.ylabel(纵坐标标签)
	plt.savefig("%s.pdf" % 测量指标, bbox_inches = "tight")
	plt.close()
